<template>
  <el-dialog
    model-value="dialogVisible"
    width="30%"
    :title="dialogTitle"
    @close="handleClose"
  >
    <el-form
      ref="formRef"
      :model="form"
      :rules="rules"
      label-width="100px"
    >
      <el-form-item label="角色名" prop="name">
        <el-input v-model="form.name"/>
      </el-form-item>
      <el-form-item label="权限字符" prop="code">
        <el-input v-model="form.code"/>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="form.remark" type="textarea" :rows="4"/>
      </el-form-item>
    </el-form>

    <template #footer>
      <span class="dialog-footer">
        <el-button type="primary" @click="handleConfirm">确认</el-button>
        <el-button @click="handleClose">取消</el-button>
      </span>
    </template>
  </el-dialog>
    <!-- model-value:控制对话框是否可见 -->
</template>

<script setup>
import { ref, watch, defineEmits, defineProps } from 'vue'
import { ElMessage } from 'element-plus'
import requestUtil, { getServerUrl } from '@/util/request'
import { ElForm, ElFormItem, ElInput, ElButton } from 'element-plus'

const form = ref({
  id: -1,
  name: '',
  code: '',
  remark: ''
})
const formRef = ref(null)
const props = defineProps(
  {
  id: {
    type: Number,
    default: -1,
    required: true
  },
  dialogTitle: {
    type: String,
    default: '',
    required: true
  },
  dialogVisible: {
    type: Boolean,
    default: false,
    required: true
  }
}
)

const rules = ref({
  name: [{ required: true, message: '请输入角色名', trigger: 'blur' }],
  code: [{ required: true, message: '请输入权限字符', trigger: 'blur' }],
  remark: [{ required: false, message: '请输入备注', trigger: 'blur' }]
})

const initFormData = async (id) => {
  const res = await requestUtil.get("system/role?id=" + id);
  form.value = res.results[0];
}

watch(
    () => props.dialogVisible,
    () => {
      let id = props.id;
      console.log("id:" + id)
      if (id != -1) {
        initFormData(id)
      } else {
        form.value = {
          id: -1,
          name: '',
          code: '',
          remark: ''
        }
      }
    }
)

const emits = defineEmits(['update:modelValue', 'initRoleList'])

const handleClose = () => {
  emits('update:modelValue', false)
}

const handleConfirm = () => {
  formRef.value.validate(async (valid) => {
    let result
    if (valid) {
      try {
        if (form.value.id === -1) {
          // 新增
          result = await requestUtil.post('system/role/', form.value)
        } else {
          // 更新
          result = await requestUtil.patch('system/role/' + form.value.id + "/", form.value);
        }
        let data = result.data;
        ElMessage.success(form.value.id === -1 ? '添加成功' : '更新成功');
        formRef.value.resetFields()
        emits("initRoleList")
        handleClose()
      } catch(error) {
        console.error(error)
        ElMessage.error("执行失败！")
      }
    } else {
      ElMessage.error("表单验证失败！")
    }
  })
}
</script>

<style scoped>
</style>
